﻿namespace PrismEx.SampleApp.Modules.ParameterizedNavigation
{
    using System.ComponentModel.Composition;
    using System.Linq;

    using AutoMapper;

    using Microsoft.Practices.Prism.Regions;

    using PrismEx.Base;
    using PrismEx.SampleApp.Model;

    [Export]
    public class DetailViewModel : Observable, INavigationAware
    {
        private readonly CustomerRepository repository;

        private int customerId;

        private string email;

        private string firstName;

        private string gender;

        private string lastName;

        private string phone;

        static DetailViewModel()
        {
            Mapper.CreateMap<Customer, DetailViewModel>();
            Mapper.CreateMap<DetailViewModel, Customer>();
        }

        [ImportingConstructor]
        public DetailViewModel(CustomerRepository repository)
        {
            this.repository = repository;
        }

        public string Email
        {
            get
            {
                return this.email;
            }
            set
            {
                this.email = value;
                this.OnPropertyChanged(() => this.Email);
            }
        }

        public string FirstName
        {
            get
            {
                return this.firstName;
            }
            set
            {
                this.firstName = value;
                this.OnPropertyChanged(() => this.FirstName);
            }
        }

        public string Gender
        {
            get
            {
                return this.gender;
            }
            set
            {
                this.gender = value;
                this.OnPropertyChanged(() => this.Gender);
            }
        }

        public string LastName
        {
            get
            {
                return this.lastName;
            }
            set
            {
                this.lastName = value;
                this.OnPropertyChanged(() => this.LastName);
            }
        }

        public string Phone
        {
            get
            {
                return this.phone;
            }
            set
            {
                this.phone = value;
                this.OnPropertyChanged(() => this.Phone);
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            var customerId = int.Parse(navigationContext.Parameters["CustomerId"]);
            return this.customerId == customerId;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            this.customerId = int.Parse(navigationContext.Parameters["CustomerId"]);
            var customer = this.repository.GetCustomers().FirstOrDefault(c => c.Id == customerId);
            Mapper.Map(customer, this);
        }
    }
}